/*
 * @Description: 配置
 * @Author: chenju
 * @Date: 2022-01-24 14:56:31
 * @LastEditors: qingyang
 * @LastEditTime: 2023-07-20 18:03:30
 */
import { reactive, markRaw } from 'vue';
import { SearchOptionsType } from '@/components/CPageTable';

import { GetTaskDataListParams } from '@/apis/eci/carbonManagement/model/dataListModel';
import { removeUndefinedParams } from '@/utils/index';
import { getCurrentOrganizationList } from '@/apis/common/common'; // 请求的后端接口
import { useRoute } from 'vue-router';
import { CTableProps } from '@/components/CTable/index';

import { BasicPagingListParams } from '@/apis/basicModel';
import { getTaskDataList } from '@/apis/eci/carbonManagement/dataList';
import { hasBtnPermission } from '@/directives/btnPermission';
export const useDataConfig = () => {
  // 搜索字段及其他
  const searchForm = reactive<GetTaskDataListParams>({
    organizationName: '',
    inventoryYear: ''
  });
  const route = useRoute();
  // 页面标题
  const taskNo = route.query.taskNo as string;
  const title = route.query.title as string;
  const optionsTitle = taskNo + ' ' + title;

  // 处理参数
  const handleParams = (): GetTaskDataListParams => {
    // 参数
    let params: GetTaskDataListParams = {
      organizationName: searchForm.organizationName,
      inventoryYear: searchForm.inventoryYear,
      beginTime: searchForm.createTime?.length ? searchForm.createTime[0] : undefined,
      endTime: searchForm.createTime?.length ? searchForm.createTime[1] : undefined
    };
    params = removeUndefinedParams(params);
    return params;
  };

  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '组织名称',
      placeholder: '组织名称',
      field: 'organizationName',

      type: 'select',
      remoteMethod: (query: string): Promise<any> => {
        console.log('remoteMethod', query);
        const params = {
          organizationName: query
        };
        return getCurrentOrganizationList(params).then(res => {
          console.log('res', res);
          const ans = res.map(item => {
            return {
              label: item.organizationName,
              value: item.organizationName
            };
          });
          return ans;
        });
      },
      style: 'width: 192px'
    },
    {
      name: '盘查周期',
      field: 'inventoryYear',
      type: 'datePicker',
      dateType: 'year',
      format: 'YYYY'
    },
    {
      name: '创建时间',
      field: 'createTime',
      type: 'datePicker',
      startPlaceholder: '创建起始时间',
      endPlaceholder: '创建截止时间',
      dateType: 'datetimerange',
      format: 'YYYY-MM-DD HH:mm:ss'
    }
  ]);
  // 获取信息
  function loadData(parameter: BasicPagingListParams) {
    const params: GetTaskDataListParams & BasicPagingListParams = Object.assign(
      handleParams(),
      parameter
    );
    return getTaskDataList(params).then(res => {
      console.log('年度盘查清册及报告主列表返回', res);
      if (res && res.records.length) {
        return {
          records: res.records,
          total: res.total,
          pages: res.pages
        };
      } else {
        return {
          records: [],
          total: 0,
          pages: 0
        };
      }
    });
  }
  const tableOptions: CTableProps = {
    loadData,
    selection: hasBtnPermission('ECI_MERGE_REPORT_DOWNLOAD')
  };
  return {
    searchForm,
    handleParams,
    searchOptions,
    optionsTitle,
    tableOptions
  };
};
